﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ClassModel;
using System.Data.SqlClient;

namespace ClassDAL
{
    public class t_teacher_centralize_DAL
    {
        /// <summary>
        /// 插入老师集中排课数据
        /// </summary>
        /// <param name="centralize"></param>
        /// <returns></returns>
        public static int InsertCentralize(t_teacher_centralize centralize)
        {

            // 删除之前的数据
            DeletePrevData(centralize.userId, centralize.fileId);

            string insertSQL = "insert into t_teacher_centralize values(@userid,@fileid,@taskid,@tids);";

            SqlParameter[] insertParams = new SqlParameter[] 
            {
                new SqlParameter("@userid",centralize.userId),
                new SqlParameter("@fileid",centralize.fileId),
                new SqlParameter("@taskid",centralize.taskId),
                new SqlParameter("@tids",centralize.tids)
            };

            return SQLHelper.ExecuteNonQuery(SQLHelper.GetConnection(), System.Data.CommandType.Text, insertSQL, insertParams);
        }

        /// <summary>
        /// 删除之前的集中排课数据
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="fileid"></param>
        /// <returns></returns>
        private static int DeletePrevData(int userid, int fileid)
        {
            string deleteSQL = "delete from t_teacher_centralize where userid = @userid and fileid = @fileid;";
            SqlParameter[] deleteParams = new SqlParameter[] 
            {
                new SqlParameter("@userid",userid),
                new SqlParameter("@fileid",fileid)
            };

            return SQLHelper.ExecuteNonQuery(SQLHelper.GetConnection(), System.Data.CommandType.Text, deleteSQL, deleteParams);
        }


        /// <summary>
        /// 获取老师集中排课数据
        /// </summary>
        /// <param name="fileId"></param>
        /// <returns></returns>
        public static t_teacher_centralize getSingleByFileId(int fileId,int userId)
        {
            string getSQL = "select id,userid,taskid,fileid,tids from t_teacher_centralize where fileid = @fileId and userid = @userId;";
            SqlParameter[] getParams = new SqlParameter[] 
            {
                new SqlParameter("@fileId",fileId),
                new SqlParameter("@userId",userId)
            };

            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.GetConnection(), System.Data.CommandType.Text,getSQL,getParams))
            {
                if (!reader.Read()) return null;

                t_teacher_centralize ttc = new t_teacher_centralize();
                ttc.id = Convert.ToInt32(reader["id"]);
                ttc.userId = Convert.ToInt32(reader["userid"]);
                ttc.taskId = Convert.ToInt32(reader["taskid"]);
                ttc.fileId = Convert.ToInt32(reader["fileid"]);
                ttc.tids = reader["tids"].ToString();

                return ttc;
            }
        }
    }
}
